﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MapaMovil
{
    using System;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Diagnostics;
    using System.Windows.Forms;

    //BATTMETER CLASS

    public class BattMeter
    {

        public class SYSTEM_POWER_STATUS_EX2
        {
            public byte ACLineStatus;
            public byte BatteryFlag;
            public byte BatteryLifePercent;
            public byte Reserved1;
            public uint BatteryLifeTime;
            public uint BatteryFullLifeTime;
            public byte Reserved2;
            public byte BackupBatteryFlag;
            public byte BackupBatteryLifePercent;
            public byte Reserved3;
            public uint BackupBatteryLifeTime;
            public uint BackupBatteryFullLifeTime;
            public uint BatteryVoltage;
            public uint BatteryCurrent;
            public uint BatteryAverageCurrent;
            public uint BatteryAverageInterval;
            public uint BatterymAHourConsumed;
            public uint BatteryTemperature;
            public uint BackupBatteryVoltage;
            public byte BatteryChemistry;
        }

        [DllImport("coredll")]
        private static extern uint GetSystemPowerStatusEx2(SYSTEM_POWER_STATUS_EX2 lpSystemPowerStatus, uint dwLen, bool fUpdate);

        private int retorna;
        public BattMeter()
        {
            this.retorna = 255;
        }
        public int Porcentaje(int cual){
            SYSTEM_POWER_STATUS_EX2 status = new SYSTEM_POWER_STATUS_EX2();

            if (GetSystemPowerStatusEx2(status, (uint)Marshal.SizeOf(status), true) == (uint)Marshal.SizeOf(status))
            {
                this.retorna=255;
                switch (cual){
                    case 1:
                        this.retorna = Convert.ToInt32(status.BatteryLifePercent);
                        break;
                    case 2:
                        this.retorna = Convert.ToInt32(status.BatteryVoltage);
                        break;
                    case 3:
                        this.retorna = Convert.ToInt32(status.BatteryCurrent);
                        break;
                    case 4:
                        this.retorna = Convert.ToInt32(status.BatterymAHourConsumed);
                        break;
                    case 5:
                        this.retorna = Convert.ToInt32(status.BatteryTemperature);
                        break;
                    default:
                        this.retorna = 255;
                        break;
                }
                return this.retorna;
            }
            return 255;
        }

        public String getDate()
        {
            DateTime time = DateTime.Now;
            String date = time.Day + "/" + time.Month + "/" + time.Year + " " + time.Hour + ":" + time.Minute + ":" + time.Second;
            Debug.Write(date + "\n");
            return date;
        }


    }
}
